package impl

import (
	"adam2/internal/dto"
	"adam2/internal/model"
	"adam2/internal/vo"
	pConstants "anubis-framework/pkg/constants"
	pDomain "anubis-framework/pkg/domain"
	"time"
)

type MonitorCurrentPriceServiceImpl struct {
	*BaseServiceImpl
}

func GetMonitorCurrentPriceServiceImpl() *MonitorCurrentPriceServiceImpl {
	return &MonitorCurrentPriceServiceImpl{GetBaseServiceImpl()}
}

// 添加
func (_monitorCurrentPriceServiceImpl MonitorCurrentPriceServiceImpl) Add(addMonitorCurrentPriceVo *vo.AddMonitorCurrentPriceVo) *pDomain.ServiceResult {
	var monitorCurrentPrice model.MonitorCurrentPrice = model.MonitorCurrentPrice{}
	monitorCurrentPrice.MonitorPrice = addMonitorCurrentPriceVo.MonitorPrice
	monitorCurrentPrice.Name = addMonitorCurrentPriceVo.Name
	monitorCurrentPrice.Code = addMonitorCurrentPriceVo.Code
	monitorCurrentPrice.Available = addMonitorCurrentPriceVo.Available
	monitorCurrentPrice.Comparison = addMonitorCurrentPriceVo.Comparison
	monitorCurrentPrice.TransactionType = addMonitorCurrentPriceVo.TransactionType
	monitorCurrentPrice.CreateTime = time.Now()

	_monitorCurrentPriceServiceImpl._monitorCurrentPriceDaoImpl.Add(&monitorCurrentPrice)

	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	return serviceResult
}

// 根据transactionType、code、name、comparison、monitorPrice、available判断记录是否已经存在
func (_monitorCurrentPriceServiceImpl MonitorCurrentPriceServiceImpl) ExistByTransactionTypeAndCodeAndNameAndComparisonAndMonitorPriceAndAvailable(transactionType int, code string, name string, comparison int, monitorPrice float64, available int) *pDomain.ServiceResult {
	var _monitorCurrentPrice model.MonitorCurrentPrice = model.MonitorCurrentPrice{}
	_monitorCurrentPrice.TransactionType = transactionType
	_monitorCurrentPrice.Code = code
	_monitorCurrentPrice.Name = name
	_monitorCurrentPrice.Comparison = comparison
	_monitorCurrentPrice.Available = available
	var monitorCurrentPriceArray model.MonitorCurrentPriceArray = _monitorCurrentPriceServiceImpl._monitorCurrentPriceDaoImpl.Find(_monitorCurrentPrice)
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	if len(monitorCurrentPriceArray) != 0 {
		serviceResult.Code = pConstants.OPERATION_FAIL_CODE
		serviceResult.Message = "参数transactionType、code、name、comparison、monitorPrice、available重复"
		serviceResult.Success = false
		return serviceResult
	} else {
		serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
		serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
		serviceResult.Success = true
		serviceResult.Result = monitorCurrentPriceArray
		return serviceResult
	}
}

// 分页显示
func (_monitorCurrentPriceServiceImpl MonitorCurrentPriceServiceImpl) Page(transactionType int, code string, name string, available int, pageNo int, pageSize int) *pDomain.ServiceResult {
	var monitorCurrentPriceDtoArray dto.MonitorCurrentPriceDtoArray = _monitorCurrentPriceServiceImpl._monitorCurrentPriceDaoImpl.Page(transactionType, code, name, available, pageNo, pageSize)
	var total int = _monitorCurrentPriceServiceImpl._monitorCurrentPriceDaoImpl.PageTotal(transactionType, code, name, available)
	var pageCount int
	if total%pageSize == 0 {
		pageCount = total / pageSize
	} else {
		pageCount = total/pageSize + 1
	}
	var pageDto dto.PageDto = dto.PageDto{monitorCurrentPriceDtoArray, total, pageCount}
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	serviceResult.Result = pageDto
	return serviceResult
}

// 根据id更新available
func (_monitorCurrentPriceServiceImpl MonitorCurrentPriceServiceImpl) UpdateAvailableById(id int, available int) *pDomain.ServiceResult {
	_monitorCurrentPriceServiceImpl._monitorCurrentPriceDaoImpl.UpdateAvailableById(id, available)
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	return serviceResult
}

// 删除监控
func (_monitorCurrentPriceServiceImpl MonitorCurrentPriceServiceImpl) DeleteMonitorCurrentPrice(id int) *pDomain.ServiceResult {
	// 删除监控
	_monitorCurrentPriceServiceImpl._monitorCurrentPriceDaoImpl.DeleteMonitorCurrentPrice(id)
	var serviceResult *pDomain.ServiceResult = &pDomain.ServiceResult{}
	serviceResult.Code = pConstants.OPERATION_SUCCESS_CODE
	serviceResult.Message = pConstants.OPERATION_SUCCESS_MESSAGE
	serviceResult.Success = true
	return serviceResult
}
